import { useSetAtom } from "jotai";
import { Suspense } from "react";
import { RouterProvider } from "react-router/dom";
import router from "@/router";
import NavigateProgress from "./components/NavigateProgress";
import { authAtom } from "./store/user";
import useOnMounted from "./lib/hooks/useOnMounted";
import { useMessage } from "./providers/AntdMessageProvider";

export default function App() {
  const auth = useSetAtom(authAtom);
  const message = useMessage();

  useOnMounted(() => {
    const abortController = new AbortController();
    auth(abortController.signal).then((user) => {
      if (user) {
        message.success(`欢迎回来，${user.nickName}!`);
      }
    });

    return () => abortController.abort("Cancel");
  });

  return (
    <Suspense fallback={<NavigateProgress />}>
      <RouterProvider router={router}></RouterProvider>
    </Suspense>
  );
}
